package com.spring.sunflower.conversation;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.spring.sunflower.R;
import com.spring.sunflower.bean.CustomMessageBean;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.util.DateTimeUtils;
import com.spring.sunflower.util.LogUtils;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMMessage;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Date;
import java.util.List;

/**
 * Created by Kevin on 2021/6/4<br/>
 * <p>
 * Describe:<br/>
 */
public class MessageAdapter extends BaseQuickAdapter<V2TIMConversation, BaseViewHolder> {
    public MessageAdapter(int layoutResId, @Nullable List<V2TIMConversation> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, V2TIMConversation v2TIMConversation) {
        Glide.with(getContext())
                .load(v2TIMConversation.getFaceUrl())
                .error(R.drawable.ic_placeholder)
                .into((ImageView) baseViewHolder.getView(R.id.ivCover));
        baseViewHolder.setText(R.id.tvName, v2TIMConversation.getShowName());
        V2TIMMessage lastMessage = v2TIMConversation.getLastMessage();
        if (lastMessage == null) return;
        baseViewHolder.setText(R.id.tvLastMessage, ConversationManager.getLastMessage(lastMessage));
        long lastMessageTimestamp = lastMessage.getTimestamp();
        TextView msgCount = baseViewHolder.getView(R.id.tvMessageCount);
        int unreadCount = v2TIMConversation.getUnreadCount();
        LogUtils.INSTANCE.logD("Message", v2TIMConversation.getUserID() + "=unreadCount=" + unreadCount+"lastMsg="+v2TIMConversation.getLastMessage());
        if (unreadCount <= 0) {
            msgCount.setVisibility(View.GONE);
        } else {
            msgCount.setVisibility(View.VISIBLE);
            baseViewHolder.setText(R.id.tvMessageCount, unreadCount + "");
        }
        if (lastMessage != null) {
            baseViewHolder.setText(R.id.tvTime, DateTimeUtils.getTimeFormatText(new Date(lastMessageTimestamp * 1000)));
        } else {
            baseViewHolder.setText(R.id.tvTime, "");
        }
        if (v2TIMConversation.isPinned()) {
            LogUtils.INSTANCE.logD("MessageAdapter", "xxx=" + v2TIMConversation.getUserID());
            baseViewHolder.setBackgroundColor(R.id.clContainer, ContextCompat.getColor(getContext(), R.color.backgroundColor));
        } else {
            baseViewHolder.setBackgroundColor(R.id.clContainer, ContextCompat.getColor(getContext(), R.color.white));
        }


    }

    private String getLastMessage(V2TIMMessage lastMessage) {
        if (lastMessage == null) return "";
        CustomMessageBean bean = ConversationManager.processV2TIMMessage(lastMessage);
//        MessageCustomEntity messageCustomEntity = ConversationManager.customMessageBean2MessageCustomEntity(bean, lastMessage);
        LogUtils.INSTANCE.logI("MessageAdapter", "eleType=" + lastMessage.getElemType());
        if (bean == null) return "";
        CustomMessageBean.DataBean data = bean.data;
        String msg = "";
        LogUtils.INSTANCE.logI("MessageAdapter", "content=" + data.content);
        switch (data.msgType) {
            case Constant.CUSTOM_MESSAGE_TEXT:
                msg = data.content;
                break;
            case Constant.CUSTOM_MESSAGE_PICTURE:
                msg = "[图片]";
                break;
            case Constant.CUSTOM_MESSAGE_SOUND:
                msg = "[语音]";
                break;
            case Constant.CUSTOM_MESSAGE_VIDEO:
                msg = "[视频]";
                break;
            case Constant.CUSTOM_MESSAGE_FILE:
                msg = "[文件]";
                break;
            case Constant.CUSTOM_MESSAGE_LOCATION:
                msg = "[位置]";
                break;
            case Constant.CUSTOM_MESSAGE_GIFT:
                msg = "[礼物]";
                break;
            case Constant.CUSTOM_MESSAGE_LOCAL_TIME:
                msg = "[提示消息]";
                break;
            default:
                msg = "[不支持的消息类型]";
                break;
        }

        return msg;
    }
}
